<template>
  <view class="login-container">
    <uni-icons type="contact-filled" size="100" color="#AFAFAF"></uni-icons>
    <button class="btn-login" @click="wxLogin">一键登录</button>
    <text class="tips-text">登录后尽享更多权益</text>
  </view>
</template>

<script>
import { wxLoginApi } from '@/api/index'
import { mapMutations, mapGetters } from 'vuex'
export default {
  computed: {
    ...mapGetters('user', ['getRedirectInfo'])
  },
  methods: {
    ...mapMutations('user', ['setuserInfo', 'setToken']),
    async wxLogin () {
      const [err, res] = await uni.getUserProfile({ desc: '为了更好的为您服务' })
      if (err) return

      const { encryptedData, rawData, iv, signature } = res
      const [, { code }] = await uni.login()
      this.setuserInfo(res.userInfo)

      const data = {
        encryptedData,
        rawData,
        iv,
        signature,
        code
      }

      const { meta: { status } } = await wxLoginApi(data)

      // 此处后端问题，没办法获取到TOKEN，为了项目进行，此处忽视token问题
      if (status === 400) {
        uni.showToast({
          title: '登录成功',
          duration: 800,
          icon: 'success',
          mask: true
        })

        setTimeout(() => {
          this.setToken('123456789123456789')
          if (this.getRedirectInfo) {
            uni.switchTab({
              url: this.getRedirectInfo
            })
          }
        }, 800);
      }

    }
  }
}
</script>

<style lang="scss" scoped>
.login-container {
  // 登录盒子的样式
  height: 750rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background-color: #f8f8f8;
  position: relative;
  overflow: hidden;

  // 绘制登录盒子底部的半椭圆造型
  &::after {
    content: ' ';
    display: block;
    position: absolute;
    width: 100%;
    height: 40px;
    left: 0;
    bottom: 0;
    background-color: white;
    border-radius: 100%;
    transform: translateY(50%);
  }

  // 登录按钮的样式
  .btn-login {
    width: 90%;
    border-radius: 100px;
    margin: 15px 0;
    background-color: #c00000;
    color: white;
  }

  // 按钮下方提示消息的样式
  .tips-text {
    font-size: 12px;
    color: gray;
  }
}
</style>
